"use strict";
cc._RF.push(module, '8c81dBMY8VMTIf9iTlJcRGc', 'GameEventHandler');
// scripts/game/GameEventHandler.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameEventHandler = void 0;
var GameBean_1 = require("../bean/GameBean");
var EventCenter_1 = require("../common/EventCenter");
var GameMgr_1 = require("../common/GameMgr");
/**
 * 游戏事件处理器
 * 负责处理来自服务器的游戏事件消息，并转发给相应的处理器
 * 支持统一数字事件系统，处理方块动画相关事件
 */
var GameEventHandler = /** @class */ (function () {
    function GameEventHandler() {
        this.gamePageController = null;
        // 构造函数中不进行初始化，等待外部调用
    }
    /**
     * 设置游戏页面控制器
     */
    GameEventHandler.prototype.setGamePageController = function (controller) {
        this.gamePageController = controller;
    };
    /**
     * 注册WebSocket消息处理器
     */
    GameEventHandler.prototype.registerWebSocketHandlers = function () {
        // 使用事件系统监听WebSocket消息
        GameMgr_1.GameMgr.Event.AddEventListener(EventCenter_1.EventType.ReceiveMessage, this.onReceiveMessage, this);
    };
    /**
     * 处理接收到的WebSocket消息
     */
    GameEventHandler.prototype.onReceiveMessage = function (messageBean) {
        // 处理GameFrame消息，其中包含游戏事件
        if (messageBean.msgId === "GameFrame") {
            var frameData = messageBean.data;
            this.handleGameFrame(frameData);
        }
        // 可以根据需要添加其他消息类型的处理
    };
    /**
     * 处理GameFrame消息，提取其中的游戏事件
     */
    GameEventHandler.prototype.handleGameFrame = function (frameData) {
        // 处理每个游戏事件
        for (var _i = 0, _a = frameData.gameEvents; _i < _a.length; _i++) {
            var gameEvent = _a[_i];
            this.handleGameEvent(gameEvent);
        }
    };
    /**
     * 处理游戏事件，转换为标准格式
     */
    GameEventHandler.prototype.handleGameEvent = function (gameEvent) {
        // 根据事件类型处理不同的数据结构
        if (gameEvent.eventType >= 1 && gameEvent.eventType <= 6) {
            // 游戏阶段事件
            this.handlePhaseEvent(gameEvent.eventType, gameEvent);
        }
        else if (gameEvent.eventType === 7 || gameEvent.eventType === 9) {
            // 玩家死亡/离开事件
            this.handlePlayerEvent(gameEvent.eventType, gameEvent);
        }
    };
    /**
     * 处理游戏阶段事件
     */
    GameEventHandler.prototype.handlePhaseEvent = function (eventType, eventData) {
        // 直接转发事件数据，因为已经是标准格式
        this.forwardToGameController(eventData);
    };
    /**
     * 处理玩家事件
     */
    GameEventHandler.prototype.handlePlayerEvent = function (eventType, eventData) {
        // 直接转发事件数据，因为已经是标准格式
        this.forwardToGameController(eventData);
    };
    /**
     * 转发事件到游戏控制器
     */
    GameEventHandler.prototype.forwardToGameController = function (gameEventData) {
        if (!this.gamePageController) {
            console.error("GamePageController未设置，无法处理游戏事件");
            return;
        }
        this.gamePageController.handleGameEvent(gameEventData);
    };
    /**
     * 验证事件数据
     */
    GameEventHandler.prototype.validateEventData = function (eventData) {
        if (!eventData) {
            console.error("事件数据为空");
            return false;
        }
        if (typeof eventData.eventType !== 'number' || eventData.eventType < 1 || eventData.eventType > 10) {
            console.error("\u65E0\u6548\u7684\u4E8B\u4EF6\u7C7B\u578B: " + eventData.eventType);
            return false;
        }
        // 验证需要方块数据的事件类型
        if ([GameBean_1.GameEventType.SHAKING, GameBean_1.GameEventType.FALLING, GameBean_1.GameEventType.RESTORING].includes(eventData.eventType)) {
            if (!eventData.blocks || !Array.isArray(eventData.blocks)) {
                console.error("\u4E8B\u4EF6\u7C7B\u578B" + eventData.eventType + "\u9700\u8981\u65B9\u5757\u6570\u636E");
                return false;
            }
            // 验证方块数据
            for (var _i = 0, _a = eventData.blocks; _i < _a.length; _i++) {
                var block = _a[_i];
                if (!this.validateBlockData(block)) {
                    return false;
                }
            }
        }
        // 验证随机种子
        if ([GameBean_1.GameEventType.SHAKING, GameBean_1.GameEventType.FALLING, GameBean_1.GameEventType.RESTORING].includes(eventData.eventType)) {
            if (typeof eventData.randomSeed !== 'number') {
                console.error("\u4E8B\u4EF6\u7C7B\u578B" + eventData.eventType + "\u9700\u8981\u968F\u673A\u79CD\u5B50");
                return false;
            }
        }
        return true;
    };
    /**
     * 验证方块数据
     */
    GameEventHandler.prototype.validateBlockData = function (block) {
        if (!block) {
            console.error("方块数据为空");
            return false;
        }
        if (typeof block.x !== 'number' || block.x < 0 || block.x > 8) {
            console.error("\u65E0\u6548\u7684\u65B9\u5757X\u5750\u6807: " + block.x);
            return false;
        }
        if (typeof block.y !== 'number' || block.y < 0 || block.y > 8) {
            console.error("\u65E0\u6548\u7684\u65B9\u5757Y\u5750\u6807: " + block.y);
            return false;
        }
        return true;
    };
    /**
     * 创建测试事件数据（用于调试）
     */
    GameEventHandler.prototype.createTestEvent = function (eventType, blockCount) {
        if (blockCount === void 0) { blockCount = 10; }
        var testBlocks = [];
        // 生成随机测试方块
        for (var i = 0; i < blockCount; i++) {
            testBlocks.push({
                id: i + 1,
                x: Math.floor(Math.random() * 9),
                y: Math.floor(Math.random() * 9),
                blockType: 1,
                animationState: GameBean_1.BlockAnimationState.NONE,
                animationDelay: 0
            });
        }
        var testEvent = {
            eventType: eventType,
            currentRound: 1,
            countDown: 2000,
            randomSeed: Math.floor(Math.random() * 1000000),
            message: this.getEventMessage(eventType),
            blocks: [GameBean_1.GameEventType.SHAKING, GameBean_1.GameEventType.FALLING, GameBean_1.GameEventType.RESTORING].includes(eventType) ? testBlocks : undefined
        };
        return testEvent;
    };
    /**
     * 获取事件消息
     */
    GameEventHandler.prototype.getEventMessage = function (eventType) {
        switch (eventType) {
            case GameBean_1.GameEventType.WAITING:
                return "游戏即将开始";
            case GameBean_1.GameEventType.ROUND_WAITING:
                return "等待下一回合";
            case GameBean_1.GameEventType.SHAKING:
                return "方块开始松动";
            case GameBean_1.GameEventType.FALLING:
                return "方块开始掉落";
            case GameBean_1.GameEventType.RESTORING:
                return "方块开始补齐";
            case GameBean_1.GameEventType.ENDING:
                return "游戏结束";
            case GameBean_1.GameEventType.PLAYER_DEATH:
                return "玩家死亡";
            case GameBean_1.GameEventType.PLAYER_DISCONNECT:
                return "玩家断线";
            case GameBean_1.GameEventType.PLAYER_LEAVE:
                return "玩家离开";
            case GameBean_1.GameEventType.PLAYER_RECONNECT:
                return "玩家重连";
            default:
                return "未知事件";
        }
    };
    /**
     * 清理资源
     */
    GameEventHandler.prototype.cleanup = function () {
        if (this.gamePageController) {
            // 移除WebSocket消息监听
            GameMgr_1.GameMgr.Event.RemoveEventListener(EventCenter_1.EventType.ReceiveMessage, this.onReceiveMessage, this);
            this.gamePageController = null;
        }
    };
    return GameEventHandler;
}());
exports.GameEventHandler = GameEventHandler;

cc._RF.pop();